var tree = null;
var userId = "";
var projectId = getQueryString("projectId") || '88888888888';
var modelId = getQueryString("modelId") || '0';
var compDatas = "";

$(function () {
	$.ajax({
		url: "compares/getUserId",
		type: 'POST',
		cache: false,
		async: false,
		dataType: "json",
		contentType: "application/json",
		success: function (data) {
			userId = data;
		}
	});
	getSelectDatas();
	$("#indexSel").on("changed.bs.select", function (e, clickedIndex, newValue, oldValue) {
		if (compDatas != "") {
			$.when(getEconEcahtData(compDatas, this.value)).done(function (data) {
				createEchat(data, 1);
			});
			$.when(getEconTableData(compDatas, this.value)).done(function (data) {
				createTable(data, 1);
			});
		}
	});
});
function selectData() {
	$("#operate").css("display", "block");
	var compProj = $('#compProj').val();
	if (compProj.length == 0) {
		JyyMsgBox.alert({ title: "警告", message: "[对比方案]不可为空", type: "warning" }).on(function (e) { });
		return false;
	}
	compDatas = compProj;
	modelLoading("divEnvir");
	$.when(getCompTableData(compProj)).done(function (data) {
		createTable(data.respData, 0);
		var value = "";
		if (data.respData.dataList.length > 0) value = data.respData.dataList[0].name;
		$.when(getCompEcahtData(compProj, value)).done(function (data) {
			createEchat(data.respData, 0);
			endModelLoading("divEnvir");
		});
	});
	var indexSel = $("#indexSel").val();
	modelLoading("divEcon");
	$.when(getEconEcahtData(compProj, indexSel)).done(function (data) {
		createEchat(data.respData, 1);
	});
	$.when(getEconTableData(compProj, indexSel)).done(function (data) {
		createTable(data.respData, 1);
		endModelLoading("divEcon");
	});
	modelLoading("divRen");
	$.when(getRenTableData(compProj)).done(function (data) {
		createTable(data.respData, 2);
		var value = "";
		if (data.respData.dataList.length > 0) value = data.respData.dataList[0].name;
		$.when(getRenEcahtData(compProj, value)).done(function (data) {
			createEchat(data.respData, 2);
			endModelLoading("divRen");
		});
	});
	modelLoading("divSysSta");
	$.when(getSysStaTableData(compProj)).done(function (data) {
		createTable(data.respData, 3);
		var value = "";
		if (data.respData.dataList.length > 0) value = data.respData.dataList[0].name;
		$.when(getSysStaEcahtData(compProj, value)).done(function (data) {
			createEchat(data.respData, 3);
			endModelLoading("divSysSta");
		});
	});
	modelLoading("divUnit");
	$.when(getUnitHoursEcahtData(compProj)).done(function (data) {
		createEchat(data.respData, 4);
		endModelLoading("divUnit");
	});
	$.when(getTimesEcahtData(compProj)).done(function (data) {
		createEchat(data.respData, 5);
	});
	modelLoading("divSection");
	$.when(getSectionTableData(compProj)).done(function (data) {
		createTable(data.respData, 6);
		endModelLoading("divSection");
	});
}
//查询按钮
function select() {
	selectData();
}
// 对比方案下拉框数据
function getSelectDatas() {
	$.ajax({
		url: "compares/getCompDatas",
		type: 'POST',
		cache: false,
		async: false,
		dataType: "json",
		contentType: "application/json",
		data: JSON.stringify({ userId: userId, modelId: modelId }),
		success: function (data) {
			var content = '';
			$.each(data.respData.compData, function (i, n) {
				if (i == 0 || (i != 0 && n.projectId != data.respData.compData[i - 1].projectId)) {
					if (i != 0) {
						content += "</optgroup>";
					}
					content += "<optgroup label='" + n.projectName + "'>";
				}
				content += "<option value='" + n.planCode + "'>" + n.planName + "</option>";
			});
			$("#compProj").append(content + "</optgroup>");
			$("#compProj").selectpicker("refresh");
		}
	});
}
// 获取统计图和表格相关的数据
function getEcahtData(compProj) {
	var res = {};
	$.when(getCompTableData(compProj)).done(function (data) {
		res.compTableData = data.respData;
		$.when(getCompEcahtData(compProj, data.respData.dataList[0].name)).done(function (data) {
			res.compRes = data;
		});
	});
	var indexSel = $("#indexSel").val();
	$.when(getEconEcahtData(compProj, indexSel)).done(function (data) {
		res.econRes = data;
	});
	$.when(getRenTableData(compProj)).done(function (data) {
		res.renTableData = data;
		$.when(getRenEcahtData(compProj, data.dataList[0].name)).done(function (data) {
			res.renRes = data;
		});
	});
	$.when(getSysStaTableData(compProj)).done(function (data) {
		res.sysStaTableData = data;
		$.when(getSysStaEcahtData(compProj, data.dataList[0].name)).done(function (data) {
			res.sysStaRes = data;
		});
	});
	$.when(getUnitHoursEcahtData(compProj)).done(function (data) {
		res.unitHoursRes = data;
	});
	$.when(getTimesEcahtData(compProj)).done(function (data) {
		res.timesRes = data;
	});
	$.when(getSectionTableData(compProj)).done(function (data) {
		res.sectionTableData = data;
	});
	/*var sectionSel = $("#sectionSel").val();
	$.when(getSectionEcahtData(compProj, sectionSel)).done(function(data){
		res.sectionRes = data;
	});*/
	return res;
}
// 环保专题
function getCompEcahtData(compProj, name) {
	var res = $.ajax({
		url: "compares/selectCompEchatData",
		type: 'POST',
		cache: false,
		async: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj, name: name }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
function getEconTableData(compProj, indexSel) {
	var res = $.ajax({
		url: "compares/selectEconTableData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj, indexSel: indexSel }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
function getCompTableData(compProj) {
	var res = $.ajax({
		url: "compares/selectCompTableData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
// 技术经济专题
function getEconEcahtData(compProj, indexSel) {
	var res = $.ajax({
		url: "compares/selectEconEchatData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj, indexSel: indexSel }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
// 可再生能源消纳专题
function getRenEcahtData(compProj, name) {
	var res = $.ajax({
		url: "compares/selectRenEchatData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj, name: name }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
function getRenTableData(compProj) {
	var res = $.ajax({
		url: "compares/selectRenTableData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}

// 系统稳定性专题
function getSysStaEcahtData(compProj, name) {
	var res = $.ajax({
		url: "compares/selectSysStaEchatData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj, name: name }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
function getSysStaTableData(compProj) {
	var res = $.ajax({
		url: "compares/selectSysStaTableData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
// 机组运行专题
function getUnitHoursEcahtData(compProj) {
	var res = $.ajax({
		url: "compares/selectUnitHoursEchatData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
function getTimesEcahtData(compProj) {
	var res = $.ajax({
		url: "compares/selectTimesEchatData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
// 断面分析专题
function getSectionEcahtData(compProj) {
	var res = $.ajax({
		url: "../g/service/Sample/selectSectionCompEchatData",
		type: 'POST',
		cache: false,
		dataType: "json",
		data: JSON.stringify({ compProj: compProj }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
function getSectionTableData(compProj) {
	var res = $.ajax({
		url: "compares/selectSectionTableData",
		type: 'POST',
		cache: false,
		/*async:false,*/
		dataType: "json",
		data: JSON.stringify({ compProj: compProj }),
		contentType: "application/json",
		success: function (data) {
			return data;
		}
	});
	return res;
}
// 创建统计图
function createEchat(echartData, id) {
	var chartDom = null;
	var seriesData = [];
	var yAxis = [];
	var option = {};
	if (id == 0 || id == 2 || id == 3) {
		if (id == 0) {
			chartDom = document.getElementById('echartEnvir');
		} else if (id == 2) {
			chartDom = document.getElementById('echartRen');
		} else if (id == 3) {
			chartDom = document.getElementById('echartSysSta');
			echartData.unit = "";
		}
		seriesData = [{
			name: echartData.name,
			type: 'bar',
			barWidth: '40%',
			data: echartData.seriesData
		}];
		yAxis = [{
			type: 'value',
			name: echartData.unit
		}];
	} else if (id == 1 || id == 5) {
		if (id == 1) {
			chartDom = document.getElementById('echartEcon');
		} else if (id == 5) {
			chartDom = document.getElementById('echartStarStopTimes');
		}
		for (var i = 0; i < echartData.seriesData.length; i++) {
			var d = echartData.seriesData;
			seriesData.push({
				name: d[i].name,
				type: 'bar',
				stack: '总量',
				emphasis: {
					focus: 'series'
				},
				data: d[i].series
			});
		}
		yAxis = [{
			type: 'value'
		}];
	} else if (id == 4) {
		chartDom = document.getElementById('echartUnitHours');
		for (var i = 0; i < echartData.seriesData.length; i++) {
			var d = echartData.seriesData;
			seriesData.push({
				name: d[i].name,
				type: 'bar',
				emphasis: {
					focus: 'series'
				},
				data: d[i].series
			});
		}
		yAxis = [{
			type: 'value',
			name: 'H'
		}];
	}
	var myChart = echarts.init(chartDom);
	if (id == 4 || id == 5) {
		$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
			myChart.resize();
		});
	}
	option = {
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'shadow',
			},
			position: function (point, params, dom, rect, size) {
				//其中point为当前鼠标的位置，size中有两个属性：viewSize和contentSize，分别为外层div和tooltip提示框的大小
				var x = point[0]; //
				var y = point[1];
				var viewWidth = size.viewSize[0];
				var viewHeight = size.viewSize[1];
				var boxWidth = size.contentSize[0];
				var boxHeight = size.contentSize[1];
				var posX = 0; //x坐标位置
				var posY = 0; //y坐标位置

				if (x < boxWidth) { //左边放不开
					posX = 5;
				} else { //左边放的下
					posX = x - boxWidth;
				}

				if (y < boxHeight) { //上边放不开
					posY = 5;
				} else { //上边放得下
					posY = y - boxHeight;
				}

				return [posX, posY];
			}
		},
		grid: {
			left: '3%',
			right: '4%',
			bottom: '9%',
			containLabel: true
		},
		xAxis: [{
			type: 'category',
			data: echartData.xAxis
		}],
		yAxis: yAxis,
		dataZoom: [{
			start: 0,
			type: "inside"
		}, {
			start: 0,
			end: 100,
			height: 10,
			bottom: 10,
			type: "slider"
		}],
		series: seriesData
	};
	if (id == 1 || id == 4 || id == 5) {
		option.legend = { top: 10 };
	}
	myChart.clear();
	myChart.setOption(option);
	window.addEventListener("resize", () => {
		if (myChart) {
			myChart.resize()
		}
	})
}
// 创建表格
function createTable(tableData, code) {
	var id = "";
	var table = {};
	tableData.columnsList[0].cellStyle = function (value, row, index) {
		if (index == 0) {
			return { classes: "blueActive", css: { "cursor": "pointer" } };
		} else {
			return { classes: "", css: { "cursor": "pointer" } };
		}
	};
	if (code == 0) {
		id = "tableEnvir";
		table = {
			cache: false,
			fixedColumns: true,
			fixedNumber: 1,
			theadClasses: "bg-primary",
			columns: tableData.columnsList,
			data: tableData.dataList,
			onClickCell: function (field, value, row, $element) {
				if (field == "name") {
					$element.parent().parent().find('td').each(function (i) {
						$(this).removeClass("blueActive");
					});
					$element.addClass("blueActive");
					$.when(getCompEcahtData(compDatas, value)).done(function (data) {
						createEchat(data.respData, 0);
					});
				}
			}
		};
	} else if (code == 1) {
		id = "tableEcon";
		table = {
			cache: false,
			fixedColumns: true,
			fixedNumber: 1,
			theadClasses: "bg-primary",
			columns: tableData.columnsList,
			data: tableData.dataList,
			onClickCell: function (field, value, row, $element) {
			}
		};
	} else if (code == 2) {
		id = "tableRen";
		table = {
			cache: false,
			fixedColumns: true,
			fixedNumber: 1,
			theadClasses: "bg-primary",
			columns: tableData.columnsList,
			data: tableData.dataList,
			onClickCell: function (field, value, row, $element) {
				if (field == "name") {
					$element.parent().parent().find('td').each(function (i) {
						$(this).removeClass("blueActive");
					});
					$element.addClass("blueActive");
					$.when(getRenEcahtData(compDatas, value)).done(function (data) {
						createEchat(data.respData, 2);
					});
				}
			}
		};
	} else if (code == 3) {
		id = "tableSysSta";
		table = {
			cache: false,
			fixedColumns: true,
			fixedNumber: 1,
			theadClasses: "bg-primary",
			columns: tableData.columnsList,
			data: tableData.dataList,
			onClickCell: function (field, value, row, $element) {
				if (field == "name") {
					$element.parent().parent().find('td').each(function (i) {
						$(this).removeClass("blueActive");
					});
					$element.addClass("blueActive");
					$.when(getSysStaEcahtData(compDatas, value)).done(function (data) {
						createEchat(data.respData, 3);
					});
				}
			}
		};
	} else if (code == 6) {
		id = "tableSection";
		table = {
			cache: false,
			fixedColumns: true,
			fixedNumber: 1,
			theadClasses: "bg-primary",
			columns: tableData.columnsList,
			data: tableData.dataList
		};
	}
	$('#' + id).bootstrapTable('destroy').bootstrapTable(table);
	var div = document.getElementById(id).parentNode;
	if (div.scrollWidth <= div.clientWidth) {
		div.parentNode.removeChild(div.parentNode.getElementsByClassName("fixed-columns")[0]);
	}
}
function invoke(url, args, callback) {
	var r, op = {
		url: '../g/service/Sample/' + url,
		type: 'POST',
		cache: false,
		async: true,
		dataType: 'json',
		data: args ? JSON.stringify(args) : null,
		contentType: "application/json"
	};
	if (typeof callback == 'function') {
		op.success = function (data) {
			if (data.error) return alert(data.error);
			callback(data);
		}
	} else {
		op.async = false;
		op.success = function (data) {
			r = data
		};
	}
	$.ajax(op);
	if (r) if (r.error) {
		alert(r.error);
		return null;
	}
	return r;
};

function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	return r ? unescape(r[2]) : null;
}

function modelLoading(id) {
	var ctrl = document.getElementById(id + "Loading");
	if (ctrl) {
		return;
	}
	var defaults = {
		opacity: 1,//loading页面透明度
		backgroundColor: "rgba(148, 148, 148, 0.3)",//loading页面背景色 195,190,190
		borderColor: "#bbb",//提示边框颜色
		borderWidth: 1,//提示边框宽度
		borderStyle: "solid",//提示边框样式
		loadingTips: "正在加载数据，请稍候...",//提示文本
		TipsColor: "#666",//提示颜色
		delayTime: 1000,//页面加载完成后，加载页面渐出速度
		zindex: 999,//loading页面层次
		sleep: 0,//设置挂起,等于0时则无需挂起
		marginTop: 0,
		marginLeft: 0
	}
	//获取DIV宽高
	var _PageHeight = $("#" + id).height();
	var _PageWidth = $("#" + id).width();

	//在页面未加载完毕之前显示的loading Html自定义内容
	var _LoadingHtml = '<div id="' + id + 'Loading" style="position:absolute;left:0;top:0;margin-top:' + defaults.marginTop + 'px;margin-left:' + defaults.marginLeft + 'px;width:' + _PageWidth + 'px;height:' + _PageHeight + 'px;background:' + defaults.backgroundColor + ';opacity:' + defaults.opacity + ';filter:alpha(opacity=' + defaults.opacity * 100 + ');z-index:' + defaults.zindex + ';"><div id="' + id + 'LoadingTips" style="position: absolute; cursor1: wait; width: auto;border-color:' + defaults.borderColor + ';border-style:' + defaults.borderStyle + ';border-width:' + defaults.borderWidth + 'px; height:60px; line-height:60px; padding-left:45px; padding-right: 5px;border-radius:10px;  background: #ffffff url(../images/loading.gif) no-repeat 10px center; color:' + defaults.TipsColor + ';font-size:16px;">' + defaults.loadingTips + '</div></div>';

	$("#" + id).append(_LoadingHtml);

	//获取loading提示框宽高
	var _LoadingTipsH = $("#" + id + "LoadingTips").height();
	var _LoadingTipsW = $("#" + id + "LoadingTips").width();

	//计算距离，让loading提示框保持在屏幕上下左右居中
	var _LoadingTop = _PageHeight > _LoadingTipsH ? (_PageHeight - _LoadingTipsH) / 2 : 0;
	var _LoadingLeft = _PageWidth > _LoadingTipsW ? (_PageWidth - _LoadingTipsW) / 2 : 0;

	$("#" + id + "LoadingTips").css({
		"left": _LoadingLeft + "px",
		"top": _LoadingTop + "px"
	});
}
function endModelLoading(id) {
	$("#" + id + "Loading").remove();
}